//
//  MineInfoEditViewController.m
//  MJApp
//
//  Created by scjh on 15/3/27.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoEditViewController.h"

#import "MineInfoEditTableViewCell.h"
#import "MineInfoEditEndTableViewCell.h"
#import "PickerViewController.h"

@interface MineInfoEditViewController ()<UITableViewDataSource,UITableViewDelegate,HomeCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *editTableView;
@property (nonatomic,strong)NSArray *tableArray;
@property (nonatomic,strong)NSDictionary *infoDic;
@property (nonatomic,strong)NSMutableDictionary *useDic;

@end

@implementation MineInfoEditViewController

- (instancetype)initWithDic:(NSDictionary *)infoDic {
    
    if (self = [super init]) {
        
        self.infoDic = infoDic;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self Data];
    [self UI];
    [self NaviUI];
    
    
}

- (void)Data {

    self.tableArray = @[ @"*昵称",@"*年龄",@"性别",@"所在地",@"生日",@"邮箱",@"QQ",@"自我介绍" ];
    self.useDic = [@{
    self.tableArray[0]:self.infoDic[@"nick_name"],
    self.tableArray[1]:self.infoDic[@"age"],
    self.tableArray[2]:[self.infoDic[@"sex"] integerValue] == 1 ? @"男" : @"女",
    self.tableArray[3]:self.infoDic[@"address"],
    self.tableArray[4]:self.infoDic[@"birthday"],
    self.tableArray[5]:self.infoDic[@"email"],
    self.tableArray[6]:self.infoDic[@"qq"],
    self.tableArray[7]:self.infoDic[@"remark"]
    } mutableCopy];
}
- (void)UI {
    
    self.title = @"编辑个人资料";
    self.editTableView.backgroundColor = Color_Back_Black;
    self.editTableView.dataSource = self;
    self.editTableView.delegate = self;
    self.editTableView.separatorColor = [UIColor clearColor];
}

- (void)NaviUI{
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(saveButton:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void)saveButton:(UIButton *)sender {
    
    [self postData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.tableArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.tableArray.count - 1) {
        
        MineInfoEditEndTableViewCell *cell = (MineInfoEditEndTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHigh;
    }
    MineInfoEditTableViewCell *cell = (MineInfoEditTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHigh;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.tableArray.count - 1) {
        
        MineInfoEditEndTableViewCell *cell = [MineInfoEditEndTableViewCell initWithTableView:tableView];
        cell.infoDic = @{
        @"desc":self.tableArray[indexPath.row],
        @"value":self.useDic[self.tableArray[indexPath.row]],
        @"index":@(indexPath.row)
        };
        cell.delegate = self;
        return cell;
    }
    MineInfoEditTableViewCell *cell = [MineInfoEditTableViewCell initWithTableView:tableView];
    cell.infoDic = @{
    @"desc":self.tableArray[indexPath.row],
    @"value":self.useDic[self.tableArray[indexPath.row]],
    @"index":@(indexPath.row)
    };
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 2 || indexPath.row == 4) {
        
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)postData {
    
    NSDictionary *dic = @{
      @"user_id":[userInfoTool share].ID,
    @"nick_name":self.useDic[self.tableArray[0]],
          @"age":self.useDic[self.tableArray[1]],
          @"sex":@([self.useDic[self.tableArray[2]] isEqual:@"男"] ? 1 : 2),
      @"address":self.useDic[self.tableArray[3]],
     @"birthday":self.useDic[self.tableArray[4]],
        @"email":self.useDic[self.tableArray[5]],
           @"qq":self.useDic[self.tableArray[6]],
       @"remark":self.useDic[self.tableArray[7]]
       };
    [WLHttpRequestAfter userEditUserInfoWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
    
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}


- (void)homeCell:(id)cell withInfo:(id)info {
    
    NSInteger index = [info[@"index"] integerValue];
    self.useDic[self.tableArray[index]] = info[@"newString"];
}

@end
